//
//  WLSitup.m
//  专注
//
//  Created by 王亮 on 2021/11/16.
//  Copyright © 2021 专注. All rights reserved.
//

#import "WLArchive.h"
#import "AppDelegate.h"
#import "NSObject+WLModelClass.h"
@implementation WLArchive
MJCodingImplementation
#pragma mark -- 归档逻辑
/** 读取属性 */
+ (id)readModel {
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:NSStringFromClass(self)];
    id p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (!p){
        p = [[self alloc]init];
    }
    return p;
}
/** 更新或设置属性 */
+(void)updataModel:(id)model{
    id oldModel = [self readModel];
    NSArray *oldClassNameArray = [oldModel getAllProperties_model_class];
    NSArray *classNameArray = [model getAllProperties_model_class];
    for (NSString *clsName in oldClassNameArray) {
        if ([classNameArray containsObject:clsName]){
            id newObject = [model valueForKey:clsName];
            id oldObject = [oldModel valueForKey:clsName];
            if (![newObject isEqual:oldObject] && newObject != nil){
                [oldModel setValue:newObject forKey:clsName];
            }
        }
    }
    //2.获取文件路径
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:NSStringFromClass(self)];
    //3.将自定义的对象保存到文件中
    [NSKeyedArchiver archiveRootObject:oldModel toFile:path];
}
/** 退出登录 */
+(void)removeModel{
    //2.获取文件路径
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:NSStringFromClass(self)];
    //3.将自定义的对象保存到文件中
    [NSKeyedArchiver archiveRootObject:[RW_UserModel new] toFile:path];
}


@end
